<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<table class="easyui-datagrid" id="userlist" title="用户列表" 
       data-options="singleSelect:false,collapsible:true,pagination:true,url:'./user/list',method:'get',pageSize:30,toolbar:'#sysusertoolbar',onClickRow: onClickRow">
    <thead>
        <tr>
        	<th data-options="field:'ck',checkbox:true"></th>
        	<th data-options="field:'id',width:200">用户ID</th>
            <th data-options="field:'username',width:200,editor:{type:'textbox',options:{prompt:'输入用户名'}}">用户名</th>
            <th data-options="field:'password',width:100,editor:{type:'textbox',options:{prompt:'输入密码'}}">用户密码</th>
            <th data-options="field:'phone',width:200,editor:{type:'textbox',options:{prompt:'输入手机号',validType:'number'}}">手机号</th>
            <th data-options="field:'email',width:300,editor:{type:'textbox',options:{prompt:'输入电子邮件',validType:'email'}}">电子邮箱</th>
            <th data-options="field:'role',width:120,align:'right',editor:{type:'combobox',options:{
							valueField:'roleId',
							textField:'roleName',
							data:[{'roleId':'1','roleName':'系统管理员'},{'roleId':'2','roleName':'普通管理员'},{'roleId':'3','roleName':'发货管理员'}],
							required:true
						}},formatter:TAOTAO.formatUerRole">角色</th>
            <th data-options="field:'created',width:130,align:'center',formatter:TAOTAO.formatDateTime">创建日期</th>
            <th data-options="field:'updated',width:130,align:'center',formatter:TAOTAO.formatDateTime">更新日期</th>
        </tr>
    </thead>
</table>
<div id="sysusertoolbar" style="padding:5px;height:auto">
		<div style="margin-bottom:5px">
			<a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="append()">添加</a>
			<a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="accept()">保存</a>
			<a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="reject()">删除</a>
		</div>
		<div>
			<lable>用户名:</lable>
			<input class="easyui-textbox" style="width:280px;height:25px" id="selectuser">
			<a href="#" class="easyui-linkbutton" iconCls="icon-search" onclick="selectUserInfo()" >查询</a>
		</div>
</div>
<script>
function selectUserInfo(){
	var selectuser = $("#selectuser").val()
	$('#userlist').datagrid('load',{
		"selectUser": selectuser,
	});
}
var editIndex = undefined;
function endEditing(){
	if (editIndex == undefined){return true}
	if ($('#userlist').datagrid('validateRow', editIndex)){
		$('#userlist').datagrid('endEdit', editIndex);
		editIndex = undefined;
		return true;
	} else {
		return false;
	}
}

function onClickRow(index){
	if (editIndex != index){
		if (endEditing()){
			$('#userlist').datagrid('selectRow', index)
					.datagrid('beginEdit', index);
			editIndex = index;
		} else {
			$('#userlist').datagrid('selectRow', editIndex);
		}
	}
}
function append(){
	if (endEditing()){
		$('#userlist').datagrid('appendRow',{created:new Date(),updated:new Date()});
		editIndex = $('#userlist').datagrid('getRows').length-1;
		$('#userlist').datagrid('selectRow', editIndex)
				.datagrid('beginEdit', editIndex);
	}
}
function removeit(){
	if (editIndex == undefined){return}
	$('#dg').datagrid('cancelEdit', editIndex)
			.datagrid('deleteRow', editIndex);
	editIndex = undefined;
}
function accept(){
	if (endEditing()){
		var rows = $('#userlist').datagrid('getChanges');
		for(var i =0;i<rows.length;i++){
			rows[i].created = new Date();
			rows[i].updated = new Date();
			$.ajax({
				 url:'./user/andOrUpdateUser',
				 method:'post',
				 data:rows[i],
				 success:function(){
					$('#userlist').datagrid('acceptChanges');
					editIndex = undefined;
					$('#userlist').datagrid('reload');
				 },
				 error:function(){
						$('#userlist').datagrid('acceptChanges');
						editIndex = undefined;
					alert("更新失败") 
				 }
			 })
		}
		
	}
}
function reject(){

	var ids = getSelectionsIds()
	 $.ajax({
		 url:'./user/delete',
		 method:'post',
		 data:{"ids":ids},
		 success:function(res){
				alert("成功") 
				$('#userlist').datagrid('reload');
		 },
		 error:function(){
			alert("重新添加，操作失败") 
		 }
	 })

}
function getSelectionsIds(){
	var itemList = $("#userlist");
	var sels = itemList.datagrid("getSelections");
	var ids = [];
	for(var i in sels){
		ids.push(sels[i].id);
	}
	ids = ids.join(",");
	return ids;
}
function getChanges(){
	var rows = $('#dg').datagrid('getChanges');
	alert(rows.length+' rows are changed!');
}
</script>